import os

import pandas as pd

from pyecharts import options as opt
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
from pyecharts.charts import Line
from pyecharts.charts import Pie

class CStockDisplay:
    html_dir = ""

    def __init__(self, ) -> None:

        CStockDisplay.html_dir = os.getcwd() + "/html/"
        if not os.path.isdir(CStockDisplay.html_dir):
            os.mkdir(CStockDisplay.html_dir)
        pass


    def display_stock_market_value_summary(self, market_value_dict : dict):
        labels_list = list(market_value_dict.keys())
        counts_list = []
        for key in market_value_dict:
            counts_list.append(len(market_value_dict[key]))

        # 创建饼图对象
        pie_obj = Pie()

        # 添加数据
        pie_obj.add("", [list(z) for z in zip(labels_list, counts_list)])

        # 设置全局配置项  
        pie_obj.set_global_opts(title_opts=opt.TitleOpts(title="股票市值分布图"),
                                legend_opts=opt.LegendOpts(orient="vertical", pos_right="0%", pos_top="5%"))
        pie_obj.set_series_opts(label_opts=opt.LabelOpts(formatter="{b}({c})"))

        pie_obj.render(CStockDisplay.html_dir + "股票市值分布图.html")
        os.system(CStockDisplay.html_dir + "股票市值分布图.html")

    def display_bar(bar_name:str, yaxis_name:str, title_name:str, xaxis_list:list, yaxis_list:list):
        # 初始化参数
        bar_init_opts = opt.InitOpts(theme=ThemeType.MACARONS, width="100%", height="600px") 
        # 创建水平条形图对象
        bar_obj = Bar(init_opts=bar_init_opts)

        bar_obj.add_xaxis(xaxis_list)
        bar_obj.add_yaxis(yaxis_name, yaxis_list)
        bar_obj.set_global_opts(xaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(rotate=-5)),
                            title_opts=opt.TitleOpts(title=title_name),
                            brush_opts=opt.BrushOpts(),
                            datazoom_opts=opt.DataZoomOpts(type_="inside"),
                            yaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(formatter="{value}")))
        
        bar_obj.render(bar_name)
        os.system(bar_name)

    def display_stock_market_value(self, market_value_dict : dict):
        for key in market_value_dict:
            temp_df = market_value_dict[key]
            name_list = temp_df["名称"].to_list()
            title_name = "市值" + key + "股票"
            yaxis_name = f'{title_name} {"("} {len(name_list)} {" 家)"}'
            # 初始化参数
            bar_init_opts = opt.InitOpts(theme=ThemeType.MACARONS, width="100%", height="600px") 
            # 创建水平条形图对象
            bar_obj = Bar(init_opts=bar_init_opts)

            bar_obj.add_xaxis(name_list)
            bar_obj.add_yaxis(yaxis_name, temp_df["总市值"].to_list())
            # bar_obj.reversal_axis() # 转变为横向条形图
            bar_obj.set_global_opts(xaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(rotate=-5)),
                                title_opts=opt.TitleOpts(title=title_name),
                                brush_opts=opt.BrushOpts(),
                                datazoom_opts=opt.DataZoomOpts(type_="inside"),
                                yaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(formatter="{value}")))
            
            bar_obj.render(CStockDisplay.html_dir + title_name + ".html")
            os.system(CStockDisplay.html_dir + title_name + ".html")


    def display_etf_trend_chart(self, etf_quote_df : pd.DataFrame):

        xdata_list = etf_quote_df["日期"].to_list()
        ydata1_list = etf_quote_df["涨跌幅"].to_list()
        ydata2_list = etf_quote_df["换手率"].to_list()
        
        ## 创建折线图对象
        line_init_opts = opt.InitOpts(width="100%", height="600px")
        line_obj = Line(init_opts=line_init_opts)

        line_obj.add_xaxis(xdata_list)
        line_obj.add_yaxis("涨跌幅", ydata1_list, label_opts=opt.LabelOpts(is_show=False))# 设置为 False 以隐藏数据标签
        line_obj.add_yaxis("换手率", ydata2_list, label_opts=opt.LabelOpts(is_show=False))# 设置为 False 以隐藏数据标签
        line_obj.set_global_opts(title_opts=opt.TitleOpts(title="成交趋势图"),
                                 yaxis_opts=opt.AxisOpts(axislabel_opts=opt.LabelOpts(formatter="{value} %")))  

        
        line_obj.render(CStockDisplay.html_dir  + "etf_trend_chart.html")
        os.system(CStockDisplay.html_dir  + "etf_trend_chart.html")